const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        city: '',
        head: '',
        name: '',
        mobile: '',
        sex:'',
        birthday: '',
        vip_name: '',
        vip_card_number: '',
        msg: '',

        select_date: '',
        select_start: '',
        select_end: '',

        date_arr: [],
        date_start: '',
        date_end: '',
        date: ''
    },
    company_intr: function () {
        wx.navigateTo({
            url: '/pages/company_intr/company_intr'
        })
    },
    callPhone: function(e) {
        var tel = wx.getStorageSync('tel');
        if(tel)
        wx.makePhoneCall({
            phoneNumber: tel,
            success: function () {},
        })
    },

    setCity: function(e){
        this.setData({
            city: e.detail.value
        })
    },

    setName: function(e){
        this.setData({
            name: e.detail.value
        })
    },

    setMobile: function(e){
        this.setData({
            mobile: e.detail.value
        })
    },

    setMsg: function(e){
        this.setData({
            msg: e.detail.value
        })
    },

    save: function(){
        var birthday = this.data.birthday, nickname = this.data.name, msg = this.data.msg;
        if(!nickname) {
            wx.showToast({
              title: '昵称不能为空',
              icon: 'none'
            });
            return false;
        }
        app.ajax({
            url: '/api/user/saveUser',
            data: {
                birthday: birthday,
                nickname: nickname,
                personal_signature: msg
            },
            success: res => {
                wx.showToast({
                  title: res.msg,
                  icon: 'none'
                });
                if(res.code == 1){
                    setTimeout(function(){
                        wx.navigateBack({
                            delta: 1,
                          })
                    }, 1500);
                }
            }
        }, 1);
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        var date_arr = [];
        for (var i = 0; i < 3; i++) {
            var now = new Date().getTime() + i * 86400000;
            date_arr.push(this.getDate(now));
        }
        console.log(date_arr);
        var start_date = this.getDate(Date.now() + 86400000 * 3),
            end_date = this.getDate(Date.now() + 86400000 * 30);
        this.setData({
            date_arr: date_arr,
            date_start: start_date.date,
            date_end: end_date.date,
            select_date: date_arr[0].date
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.userinfo();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    userinfo: function () {
        app.ajax({
            url: '/api/user/userinfo',
            success: res => {
                if (res.code == 1) {
                    wx.setStorageSync('user', res.data);
                    var is_vip = false
                    if(res.data.vip == 0)   is_vip = false
                    else  is_vip = true
                    this.setData({
                        head: res.data.avatar,
                        name: res.data.nickname,
                        mobile: res.data.mobile,
                        sex:res.data.sex,
                        birthday: res.data.birthday,
                        vip_name: res.data.vip_name,
                        vip_card_number: res.data.vip_name,
                        msg: res.data.personal_signature
                    })
                }
            }
        })
    },
    bindDateChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        var date = e.detail.value;
        this.setData({
            birthday: date,
            select_date: date
        })
    },
    getDate: function (now) {
        var date = new Date(now),
            y = date.getFullYear(),
            m = date.getMonth() + 1,
            w = date.getDay(),
            d = date.getDate(),
            weeks = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
        m = ('0' + m).slice(-2);
        d = ('0' + d).slice(-2);
        return {
            week: weeks[w],
            date: y + '-' + m + '-' + d,
            name: m + '月' + d + '日'
        };
    }
})